/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation.proportional;

import control.CBacheca;
import customGuiControl.ImagePanel;
import customGuiControl.Listner.wizardRollOverListner;
import customGuiControl.RoundedPanel2;
import entity.Messaggio;
import entity.xml.EMessaggio;
import entity.xml.EUtente;
import foundation.FMessaggi;
import foundation.FUtenti;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import org.jaxen.JaxenException;
import org.jdesktop.swingx.StackLayout;
import org.jdom.JDOMException;
import presentation.messaggioPanel;

/**
 *
 * @author Quirino
 */
public class PBacheca implements MouseListener{
    private static PBacheca istance;
    private String precTooltipText= "";
    private ArrayList<EUtente> userList;
    private EUtente currUser;
    public static PBacheca getInstance(){
        
        if(istance==null) istance= new PBacheca();
        
        return istance;
        
    }
    private JPanel panel;
    private RoundedPanel2 menuSotto;
    private RoundedPanel2 menuSottoContatto;
    private JPanel cercaBacheca;
    private JPanel modificaProfilo;
    

    public PBacheca() {
    }
    
    public static void main(String args[]) {
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
              
                try {
                    
                    FUtenti.getIstance().getListaUtenti();
                    FMessaggi.getIstance().getMessageList();
                    PBacheca.getInstance().startupProgram();
                    gui.getIstance().setVisible(true);
                    
                } catch (JDOMException ex) {
                    Logger.getLogger(PBacheca.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(PBacheca.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        });
    }
    
    public EUtente getCurrUser() {
        return currUser;
    }

    public void setCurrUser(EUtente currUser) {
        this.currUser = currUser;
    }

    public ArrayList<EUtente> getUserList() {
        return userList;
    }

    public void setUserList(ArrayList<EUtente> userList) {
        this.userList = userList;
    }
    
    public void startupProgram(){
        
        CBacheca.getIstance().loadUsers();
        this.currUser= new EUtente();
        this.currUser= CBacheca.getIstance().getCurrUser();
        this.refreshBacheca();
        
    }
    
   
    public RoundedPanel2 getMenuSotto(){return this.menuSotto;}
    
    public RoundedPanel2 getMenuSottoContatto(){ return this.menuSottoContatto;}
    
    private void refreshBacheca() {
        this.refreshPanel(null);
        //this.refreshMenuSotto();
        gui.getIstance().getBacheca().setComponentZOrder(this.getPanel(), 0);
       // gui.getIstance().setMenuSotto(this.menusotto);
    }
    
    public void refreshPanel(EUtente contatto){
         HashMap conversazioni=null;
        if(contatto==null)
           conversazioni=CBacheca.getIstance().getConversationsOfUser(this.currUser);
        else 
            conversazioni=CBacheca.getIstance().getHashMapFromArrayList(FMessaggi.getIstance().getMsgBySenderAndReceiver(this.currUser, contatto));
        
        conversazioni= CBacheca.getIstance().orderMsgByDate(conversazioni);
        
        JPanel jp= new JPanel(new relativeMarginLayout(2f,2f, 2f, 2f));
        JPanel jpInside= new JPanel(new FlowLayout(FlowLayout.LEFT));
         jpInside.setOpaque(false);
         jp.add(jpInside);
         
        gui.getIstance().getBacheca().add(jp,StackLayout.TOP);
        //jp.setBounds(50, 50, gui.getIstance().getPanelBacheca().getWidth()-100, gui.getIstance().getPanelBacheca().getHeight()-100);
        jp.setOpaque(false);
        
        Set set = conversazioni.entrySet();
        Iterator i = set.iterator(); 
        while(i.hasNext()) { 
            Map.Entry me = (Map.Entry)i.next(); 
            
            jpInside.add(new messaggioPanel((ArrayList<EMessaggio>)me.getValue(), this));
        }
        this.panel= jp;
    
    }
   public JPanel getPanel(){
        return this.panel;
    }

    
    public RoundedPanel2 getMenuSotto(gui instance){
         if(this.menuSotto==null)refreshMenuSotto(instance);
         return this.menuSotto;
     }
    public void refreshMenuSotto(gui instance) {
        /*roundedPanel rp= new roundedPanel(20, new Color(255,255,255),
                                                            new Color(229,229,229),
                                                            2f, Color.BLACK,
                                                            12114.286f,100f,Color.BLACK,
                                                            0.8f,"menuSotto");*/
        RoundedPanel2 rp=new RoundedPanel2();
        rp.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        
        gbc.gridx=0;
        gbc.gridy=0;
        gbc.weightx=0.49;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addFindBachecaButton(gbc, rp, this, instance);
        
        gbc.gridx=1;
        gbc.gridy=0;
        gbc.weightx=0.01;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addPaddingButton(gbc, rp);
       
        
        gbc.gridx=4;
        gbc.gridy=0;
        gbc.weightx=0.49;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addModificaProfiloButton(gbc, rp, this, instance);
        
        this.menuSotto= rp;
        
    }
        
    public RoundedPanel2 getMenuSottoContatto(gui instance){
         if(this.menuSottoContatto==null)refreshMenuSottoContatto(instance);
         return this.menuSottoContatto;
     }
    public void refreshMenuSottoContatto(gui instance) {
        /*RoundedPanel2 rp= new roundedPanel(20, new Color(255,255,255),
                                                            new Color(229,229,229),
                                                            2f, Color.BLACK,
                                                            12114.286f,100f,Color.BLACK,
                                                            0.8f,"menuSotto");*/
        RoundedPanel2 rp=new RoundedPanel2();
        rp.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        
        gbc.gridx=0;
        gbc.gridy=0;
        gbc.weightx=0.99;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addFindBachecaButton(gbc, rp, this, instance);
        
        
        gbc.gridx=2;
        gbc.gridy=0;
        gbc.weightx=0.01;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addPaddingButton(gbc, rp);
        
        gbc.gridx=3;
        gbc.gridy=0;
        gbc.weightx=0.99;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addModificaProfiloButton(gbc, rp, this, instance);
        
        gbc.gridx=4;
        gbc.gridy=0;
        gbc.weightx=0.01;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addPaddingButton(gbc, rp);
        
        gbc.gridx=5;
        gbc.gridy=0;
        gbc.weightx=0.99;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addScriviMessaggioButton(gbc, rp, this, instance);
        
        this.menuSottoContatto= rp;
        
    }    
    
    
    public void mouseClicked(MouseEvent e) {
        /*
        if(e.getSource().getClass().toString().contains("messaggioPanel")){
            ArrayList <EMessaggio> conv=((messaggioPanel)e.getSource()).getConversazione();
            PComponiMessaggio.getInstance().refreshPanel(null, conv);
            gui.getIstance().buttonClicked(gui._componiMessaggio);
        }
        else */
        
        if(e.getSource().getClass().toString().contains("ImagePanel")){
            
            String pathImg= ((ImagePanel) e.getSource()).getPathImg();
            
            if(pathImg.contains("scrivi_messaggio")){
                
                ArrayList<EUtente> dest= new ArrayList<EUtente>();
                dest.add(PGestioneRubrica.getInstance().getClickedUser());
                gui.getIstance().buttonClicked(gui.__scriviMessaggio);
                PComponiMessaggio.getInstance().refreshPanel(dest);
                
            }

        }
        else if(e.getSource().getClass().toString().contains("messaggioPanel")){
            
            gui.getIstance().buttonClicked(gui.__visualizzaMessaggio);
            PVisualizzaMessaggio.getInstance().visualizzaMessaggio((messaggioPanel)e.getSource());
            
        }
    }

    public void mousePressed(MouseEvent e) {
        
    }

    public void mouseReleased(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(MouseEvent e) {
        
        if(e.getSource().getClass().toString().contains("messaggioPanel")){
            this.precTooltipText= gui.getIstance().getInfoTextPane().getText();
            gui.getIstance().getInfoTextPane().setText("Clicca per visualizzare il messaggio");
        }
                
    }

    public void mouseExited(MouseEvent e) {
    
        if(e.getSource().getClass().toString().contains("messaggioPanel")){
            gui.getIstance().getInfoTextPane().setText(this.precTooltipText);
        }

    }
    
    
}
